/**
* @authors Duong NGUYEN <darl2ng@gmail.com>, Anh NGO <ngocanh.ngo@gmail.com>
* @version 0.9
* Copyright 2010
* 
* The MIT License
* Copyright (c) 2010 <Duong NGUYEN <darl2ng@gmail.com>, Anh NGO <ngocanh.ngo@gmail.com>
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package  {
	
	import com.ng.utils.c_Mediator;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class c_PropertiesMediator	extends c_Mediator{
		
		private var __Properties:c_Properties;
		private var __ResEditor:c_ResEditor;
		
		public function c_PropertiesMediator(p:c_Properties) 	{
			__Properties = p;
			__Properties.Mediator = this;
		}				
		public function set ResEditor(re:c_ResEditor):void {
			__ResEditor= re;
		}
		public function start():void {
			if (__ResEditor==null) c.e('ResEditor not available');
			__Properties.UI.__width.addEventListener(Event.CHANGE, onWidthChange);
			__Properties.UI.__height.addEventListener(Event.CHANGE, onHeigthChange);
			__Properties.UI.__scaleX.addEventListener(Event.CHANGE, onScaleXChange);
			__Properties.UI.__scaleY.addEventListener(Event.CHANGE, onScaleYChange);
			__Properties.UI.__x.addEventListener(Event.CHANGE, onXYChange);
			__Properties.UI.__y.addEventListener(Event.CHANGE, onXYChange);
			__Properties.UI.__alpha.addEventListener(Event.CHANGE, onAlphaChange);
			__Properties.UI.__rotatiton.addEventListener(Event.CHANGE, onRotationChange);
			__Properties.UI.__visible.addEventListener(MouseEvent.CLICK, onVisibleChange);
		}
		public function stop():void {
			
		}
		private function onWidthChange(e:Event):void {
			var obj: c_ResObject;
			if (__ResEditor !=null) obj = __ResEditor.currentSelect;
			if (obj != null) {
				obj.setwidth(Number(__Properties.UI.__width.text));
				if (__Properties.UI.__lock.selected == true) {
					obj.setscaleY(obj.Content.scaleX);	
				}
				__Properties.update(obj);
				__ResEditor.UpdateXML(obj.xml,obj);
			}
		}		
		private function onHeigthChange(e:Event):void {
			var obj: c_ResObject;
			if (__ResEditor !=null) obj = __ResEditor.currentSelect;
			if (obj != null) {
				obj.setheigth(Number(__Properties.UI.__height.text));

				if (__Properties.UI.__lock.selected == true) {
					obj.setscaleX(obj.Content.scaleY);
				}
				__Properties.update(obj);
				__ResEditor.UpdateXML(obj.xml,obj);
			}
		}
		private function onScaleXChange(e:Event):void {
			var obj: c_ResObject;
			if (__ResEditor !=null) obj = __ResEditor.currentSelect;
			if (obj != null) {
				obj.setscaleX(__Properties.UI.__scaleX.value);
				if (__Properties.UI.__lock.selected == true) {
					obj.setscaleY(obj.Content.scaleX);	
				}
				__Properties.update(obj);
				__ResEditor.UpdateXML(obj.xml, obj);
				
			}
		}
		private function onScaleYChange(e:Event):void {
			var obj: c_ResObject;
			if (__ResEditor !=null) obj = __ResEditor.currentSelect;
			if (obj != null) {
				obj.setscaleY(__Properties.UI.__scaleY.value);
				if (__Properties.UI.__lock.selected == true) {
					obj.setscaleX(obj.Content.scaleY);	
				}
				__Properties.update(obj);
				__ResEditor.UpdateXML(obj.xml,obj);
			}
		}
		private function onXYChange(e:Event):void {
			var obj: c_ResObject;
			if (__ResEditor !=null) obj = __ResEditor.currentSelect;
			if (obj != null) {
				obj.x = Number(__Properties.UI.__x.text);
				obj.y = Number(__Properties.UI.__y.text);
				__ResEditor.UpdateXML(obj.xml, obj);				
			}
		}
		private function onAlphaChange(e:Event):void {
			var obj: c_ResObject;
			if (__ResEditor !=null) obj = __ResEditor.currentSelect;
			if (obj != null) {
				obj.setalpha (__Properties.UI.__alpha.value);
				//__ResEditor.currentSelect.oldtransform.alphaMultiplier = __ResEditor.currentSelect.transform.colorTransform.alphaMultiplier;
				__ResEditor.UpdateXML(obj.xml, obj);
				c.d2( __ResEditor.currentSelect.transform.colorTransform.alphaMultiplier+':'+__ResEditor.currentSelect.oldtransform.alphaMultiplier);
			}
		}		
		private function onRotationChange(e:Event):void {
			var obj: c_ResObject;
			if (__ResEditor !=null) obj = __ResEditor.currentSelect;
			if (obj != null) {
				obj.setrotation(__Properties.UI.__rotatiton.value);
				__ResEditor.UpdateXML(obj.xml,obj);
			}
		}
		private function onVisibleChange(e:Event):void {
			var obj: c_ResObject;
			if (__ResEditor !=null) obj = __ResEditor.currentSelect;
			if (obj != null) {
				obj.setvisible(__Properties.UI.__visible.selected == true)
				__ResEditor.UpdateXML(obj.xml,obj);
			}
		}
	}

}